package com.kcsj.util;
//201902104058胡庆阳

import java.util.List;

/**
 *@author TG.SI@188.COM
 * @version 1.0
 * 19th Oct.2020
 *为SQL的where子句建模。
 *本类可以将符合条件的json数组转换为where子句，以便用于SQL语句的拼接本适合于字符型、数值型字段
 *未测试日期型、布尔型字段等其它类型的字段
 */

public class Condition {
    /**
     *@param condition_json多条件形成的json字串
     * 示例[{"value":"——" ,"key" "name" ,"operator":"LIKE"},
     * { "value":"9","key" :"department_id" ,"operator":"="}]
     *如果字段为字串的，无须设置模糊查询。
     * @return 多个条件对应的where子句。数值型字段也使用引号包围起来。
     * 本类依赖FastJson
     **/
    //字段名
    private final String key;
    //操作符
    private final static String operator1 = "like";
    //操作符
    //字段值
    private final Object value;
    //操作符
    private String operator;
    public static String toWhereClause(List<Condition> conditionList_json){
        //以WHERE开头，注意前后的空格
        StringBuilder sb = new StringBuilder(" WHERE ");
        //将字段名、操作符和字段值依次拼接为字符串
        for (Condition condition : conditionList_json) {
            Object value = condition.getValue();
            if(value!=null&&!value.equals("")){
                sb.append(condition.getKey()).append(" ").append
                        (condition.operator).append(" ");
                //如果操作符是like，为模糊查询
                if ("like".equalsIgnoreCase(condition.getOperator())) {
                    value = "%"+value+"%";
                }
                if(!value.toString().equals("true")&&!value.toString().equals("false")){
                    value = TxtHelper.quoteMarked(value);//把value引起来
                }
                sb.append(value).append(" AND ");
            }
        }
        //把最后一个条件后面的"and"删除
        TxtHelper.truncEnding(sb,5);
        return sb.toString();
    }

    public static String toWhereClause2(List<Condition> conditionList) {
        //以WHERE开头，注意前后的空格
        StringBuilder sb = new StringBuilder(" AND ");
        //将字段名、操作符和字段值依次拼接为字串
        //每个条件后面都附加上"AND "，注意前后的空格
        for (Condition c : conditionList) {
            Object value = c.getValue();
            if(value!=null&&!value.equals("")){
                sb.append(c.getKey()).append(" ").append(operator1).append(" ");
                value = "%" + value + "%";
                System.out.println(value.toString());
                if(!value.toString().equals("true")||!value.toString().equals("false")){
                    value = TxtHelper.quoteMarked(value);//把value引起来
                }
                sb.append(value);
                sb.append(" AND ");//注意两端的空格
            }
        }
        //把最后一个条件后面的“and”删除（删除最后5个字符)
        TxtHelper.truncEnding(sb,5);
        return sb.toString();
    }

    //不能定义无参构造器，否则FastJson会使用它来创建对象。
    public Condition(String key, String operator, Object value) {
        this.key = key;
        this.value = value;
        this.operator=operator;
    }
    public String getKey () {
        return key;
    }

    public String getOperator () {
        return operator;
    }

    public Object getValue () {
        return value;
    }

    @Override
    public String toString() {
        return "Condition{" +
                "key='" + key + '\'' +
                ", operator='" + operator + '\'' +
                ", value='" + value + '\'' +
                '}';
    }
}